package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.BehaviorStatistics;
import cn.openea.perpartner.meily.web.entity.vo.BehaviorVo;
import cn.openea.perpartner.meily.web.service.IBehaviorStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (BehaviorStatistics)表控制层
 *
 * @author makejava
 * @since 2023-05-19 10:38:19
 */
@Api(tags = "行为统计")
@RestController
@RequestMapping("behavior/statistics")
public class BehaviorStatisticsController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private IBehaviorStatisticsService behaviorStatisticsService;

    /**
     * 通过主键查询单条数据
     *
     * @param behaviorId 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("/get/{behaviorId}")
    public AjaxResult queryById(@PathVariable("behaviorId") String behaviorId) {
        if (StrUtil.isBlank(behaviorId)) {
            return AjaxResult.error("获取不到参数！");
        }
        return AjaxResult.success("查询成功！", this.behaviorStatisticsService.queryById(behaviorId));
    }

    /**
     * 更新数据（新增或修改）
     *
     * @param vo 实例对象
     * @return
     */
    @ApiOperation("更新数据")
    @PostMapping("/update")
    public AjaxResult saveOrEdit(@RequestBody BehaviorVo vo) {
        if (!this.behaviorStatisticsService.saveOrEdit(vo)) {
            return AjaxResult.error("更新出错了！");
        }
        return AjaxResult.success("更新数据成功！");
    }

    /**
     * 分页查询
     *
     * @param statistics 实例对象
     * @return
     */
    @ApiOperation("分页查询")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody BehaviorStatistics statistics) {
        startPage();
        List<BehaviorStatistics> list = this.behaviorStatisticsService.findListByVo(statistics);
        return getDataTable(list);
    }

    /**
     * 根据商品编号查询访问量
     * @param goodsNo
     * @return
     */
    @ApiOperation("根据商品编号查询访问量")
    @GetMapping("/query/{goodsNo}")
    public AjaxResult queryByGoodsNo(@PathVariable String goodsNo) {
        if(StrUtil.isBlank(goodsNo)){
            return AjaxResult.error("获取不到参数！");
        }
        return AjaxResult.success("查询成功！", this.behaviorStatisticsService.queryByGoodsNo(goodsNo));
    }
    @ApiOperation("查询访问量")
    @PostMapping("/query")
    public AjaxResult queryVisitCount(@RequestBody BehaviorStatistics  statistics) {

        return AjaxResult.success("查询成功！", this.behaviorStatisticsService.queryVisitCount(statistics));
    }
}

